$(function () {
    getProvince();
    getCity('110000');

    // var windowHeight = ((document.documentElement.clientHeight || document.body.clientHeight)-70) + 'px';
    // $('.card').css('height', windowHeight);

    $('form select').selectpicker({
        noneSelectedText: '请选择',
        sanitize: true,
        size: 12,
    });
    $('.tagsinput').tagsinput({
        maxChars: 5,
    });

    $('.bootstrap-tagsinput input').keyup(function(e) {

     }).on("compositionstart",function () {
        console.log('输入法，录入开始');
    }).on("compositionend",function () {
        console.log('输入法，输入结束');
        var realLen=$('.bootstrap-tagsinput input').val().length;
        var valString=''
        if (realLen > 5) {
            // 截取字符串
            valString = $('.bootstrap-tagsinput input').val().substring(0,5);
            // 返回
            $('.bootstrap-tagsinput input').val(valString)
        }
    })

});

// 渲染城市列表
function getProvince() {
    let provinceNames = [];
    $.each(cityJson[0], function (key, value) {
        provinceNames.push('<option value="' + key + '">' + value + '</option>')
    });

    $('#province').html(provinceNames.join(''));
    getCity('110000')
}

// 根据省获取市
function getCity(provinceId) {
    $('#city').html('');
    let key = '0,' + provinceId;
    let cityNames = [];
    $.each(cityJson[key], function (key, value) {
        cityNames.push('<option value="' + key + '">' + value + '</option>')
    });
    $('#city').html(cityNames.join(''));
}

// 切换城市
function changeCity() {
    getCity($('#province').val());
    $('form select').selectpicker('refresh');
}

// 提交
function addSubmit() {
    /*var tagStr = $('.fm-button').text().split(' ').join(',');
    var tagStrNew = tagStr.substring(0, tagStr.lastIndexOf(','))+ tagStr.substring(tagStr.lastIndexOf(',')+1);*/
    //线索池标签
    var tag = $('.tagsinput').val();
    var clueName = $('#clueName').val();
    var industry = $('#clueHangYe').val();

    //线索池名称为空和长度验证
    if (!clueName) {
        ok = false;
        promptInfo("线索池名称不能为空!");
        return;
    } else {
        if (clueName.length > 30) {
            ok = false;
            promptInfo("线索池名称长度不能大于30个字！");
            return;
        }
    }
    if (!industry) {
        ok = false;
        promptInfo("所在行业不能为空!");
        return;
    } else {
        if (industry.length > 30) {
            ok = false;
            promptInfo("所在行业长度不能大于30个字！");
            return;
        }
    }

    //标签为空判断
    if(!tag){
        promptInfo("标签不能为空！");
        return;
    }

    var jsonData = {
        cluePoolName: $('#clueName').val(),
        industry: $('#clueHangYe').val(),
        provinceId: $('#province').val(),
        provinceName: $("#province").find("option:selected").text(),
        cityId: $('#city').val(),
        cityName: $("#city").find("option:selected").text(),
        tag: tag
    };
    $.ajax({
        url: '/cluePoolPrivate/save',
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(jsonData),
        success: (function (data) {
            if (data.code == "000000") {
                $.alert("保存成功！");
                var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                var navTab = parent.$multitabs._getNavTab($thisTabs);
                closeTab(navTab);
                openTab("我的线索池","/front/user/cluePoolManager/myCluePool");

                // 提交表单后，刷新相关table页面以显示新增条目
                refreshActivePane('/front/user/cluePoolManager/myCluePool');

                delStorageAndRemove(navTab);
            } else if(data.code == "C00010"){
                $.alert("线索池名称已存在");
            }else {
                alert("保存失败");
            }
        }),
        error: (function () {
            alert("失败");
        })
    });
}


function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}